<template>
  <div v-loading="loading">
    <div class="title-gra authorityDiv">
      <span style="color:#0090ff">用户资源种类权限</span>
      <span class="right">
        <i :class="mlflag ? 'el-icon-arrow-down' : 'el-icon-arrow-up'" @click="mlflagOpen"></i>
      </span>
      <!-- <div class="grab gra-r"></div> -->
    </div>
    <div v-show="mlflag" id="mlflag">
      <el-tree
        :props="defaultProps"
        :data="treeData"
        node-key="id"
        ref="tree"
        :show-checkbox = "!view"
        @check-change="handleCheckChange"
        @node-click="handleNodeClick"
        :class="view ? 'viewcheckbox' : ''"
      ></el-tree>
      <div class="mt-20 mr-btn">
        <el-button v-if="!view" type="primary" @click="saveZYMLQX">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getAllResourceType, createAuthResCategory, roleAuthResCategory } from "@/api/authority.js";
import { deteleObject } from "@/filters/index.js";
export default {
  name: "TabDataType",
  data() {
    return {
      mlflag: true,
      treeData: [],
      loading: false,
      defaultCheck: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      labelList: [
        {
          id: "SJZYBQ1",
          name: "数据资源标签1"
        },
        {
          id: "SJZYBQ2",
          name: "数据资源标签2"
        },
        {
          id: "SJZYBQ3",
          name: "数据资源标签3"
        },
        {
          id: "SJZYBQ4",
          name: "数据资源标签4"
        },
        //{
          //id: "SJZYBQ5",
         // name: "数据资源标签5"
        //}
      ]
    };
  },
  props: ["view", "roleId"],
  methods: {
    handleNodeClick(val) {},
    handleCheckChange(val) {},
    mlflagOpen() {
      this.mlflag = !this.mlflag;
    },
    roleAuthResCategory() {
      this.loading = true;
      roleAuthResCategory(this.roleId).then(res=> {
        this.loading = false;
        if(res.success) {
          this.treeDataLevel1(res, "view");
        }
      }).catch(()=>{
        this.loading = false;
      })
    },
    async getAllResourceType() {
      this.loading = true;
      this.defaultCheck = [];
      await getAllResourceType().then(res => {
        if (res.success) {
          this.treeDataLevel1(res);
          this.getRoleTypeList();
        } else {
          this.loading = false;
        }
      }).catch(()=>{
        this.loading = false;
      });
    },
    //再次编辑时根据角色id查询对应的信息
    async getRoleTypeList() {
      await roleAuthResCategory(this.roleId).then(res=> {
        if(res.success) {
          res.data.forEach(val=>{
            if(val.wholeFlag==1) {
              this.defaultCheck.push(val.resourceId);
              }
          })
          this.$nextTick(()=>{
            this.$refs.tree.setCheckedKeys(this.defaultCheck);
          })
          this.loading = false;
        } else {
          this.loading = false;
        }
      });
    },
    treeDataLevel1(res, type) {
        let data = [];
        let dataList = [];
        dataList = res.data;
          res.data.forEach(val => {
            if (val.level == 1) {
              let map = {
                id: val.resourceId,
                label: val.resourceName,
                children: [],
                category: val.category,
                level: val.level,
                parentResourceId: val.parentResourceId,
                resourceId: val.resourceId,
                resourceName: val.resourceName
              };
              if(type) {
                map.disabled = true;
                map.id = val.id;
              }
              if(val.wholeFlag==1) {
                this.defaultCheck.push(map.id);
              }
              data.push(map);
            }
          });
          data = this.treeDataLevel2(data, dataList, type);
          data = this.treeDataLevel3(data, dataList, type);
          this.treeData = data;
    },
    treeDataLevel2(data, dataList, type) {
      data.forEach((v, i) => {
        dataList.forEach(val => {
          let value = [];
          if (v.resourceId == val.parentResourceId && val.level == 2) {
            let map = {
              id: val.resourceId,
              label: val.resourceName,
              children: [],
              category: val.category,
              level: val.level,
              parentResourceId: val.parentResourceId,
              resourceId: val.resourceId,
              resourceName: val.resourceName
            };
            //第三层增加标签
            this.labelList.forEach(v => {
              let labelMap = {
                id: v.id,
                label: v.name,
                category: val.category,
                parentResourceId: val.parentResourceId,
                resourceId: val.resourceId,
                resourceName: val.resourceName,
                children: [],
              };
              if(type) {
                labelMap.disabled = true;
                map.disabled = true;
                map.id = val.id;
              }
              if(val.wholeFlag==1) {
                this.defaultCheck.push(map.id);
              }
              map.children.push(labelMap);
            });
            v.children.push(map);
          }
        });
      });
      return data;
    },
    treeDataLevel3(data, dataList, type) {
      data.forEach((v1, i) => {
        if (v1.children.length > 0) {
          v1.children.forEach(v2 => {
            if (v2.children.length > 0) {
              v2.children.forEach(v3 => {
                dataList.forEach(val => {
                  let value = [];
                  if (
                    val.level == 3 &&
                    val.category == v3.id &&
                    val.parentResourceId == v3.resourceId
                  ) {
                    let map = {
                      id: val.resourceId,
                      label: val.resourceName,
                      children: [],
                      category: val.category,
                      level: val.level,
                      parentResourceId: val.parentResourceId,
                      resourceId: val.resourceId,
                      resourceName: val.resourceName
                    };
                    if(type) {
                      map.disabled = true;
                      map.id = val.id;
                    }
                    if(val.wholeFlag==1) {
                      this.defaultCheck.push(map.id);
                    }
                    v3.children.push(map);
                  }
                });
              });
            }
          });
        }
      });
      return data;
    },
    saveZYMLQX() {
      let seletData = this.$refs.tree.getCheckedNodes();
      let halfSelectData = this.$refs.tree.getHalfCheckedNodes();
     
      //过滤掉选中的标签数据
      seletData = seletData.filter(res=>{
        return res.level;
      });
      halfSelectData = halfSelectData.filter(res=>{
        return res.level;
      })
      //组装后台需要的数据
      let data = [];
      seletData.forEach(res => {
        let map = {
          level: res.level,
          category: res.category,
          parentResourceId: res.parentResourceId,
          resourceId: res.resourceId,
          resourceName: res.resourceName,
          roleId: this.roleId,
          wholeFlag:1
        };
        data.push(map);
      });
      halfSelectData.forEach(res => {
        let map = {
          level: res.level,
          category: res.category,
          parentResourceId: res.parentResourceId,
          resourceId: res.resourceId,
          resourceName: res.resourceName,
          roleId: this.roleId,
          wholeFlag: 0
        };
        data.push(map);
      });
      let params = {
        roleId: this.roleId,
        data: data
      }
      createAuthResCategory(params).then(res => {
        if (res.success) {
          this.$message.success("保存用户资源种类权限成功");
        } else {
          this.$message.error(res.message);
        }
      });
    }
  }
};
</script>
<style>
</style>
